package br.edu.ufcg.lsd.wbis.control.user;

import java.util.List;

import br.edu.ufcg.lsd.wbis.common.HibernateDAO;

/**
 * The implementation of the <code>UserDAO</code> interface using Hibernate
 * persistence framework
 * 
 * @author Paulo Ricardo
 * 
 */
public class HibernateUserDAO extends HibernateDAO implements UserDAO {

	public HibernateUserDAO(){
		super();
	}
	
	/**
	 * Registers a user
	 * 
	 * @param user
	 *            to be added
	 */
	@Override
	public void addUser(User user) {
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
	}

	/**
	 * Retrieves all the users that are registered
	 * 
	 * @return A list of users
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUsers() {
		session.beginTransaction();
		List<User> allusers = session.createQuery("from " + User.class.getCanonicalName()).list();
		session.getTransaction().commit();
		return allusers;
	}

	/**
	 * Removes a user that is registered
	 * 
	 * @param user
	 *            to be removed
	 */
	@Override
	public void removeUser(User user) {
		session.beginTransaction();
		session.delete(user);
		session.getTransaction().commit();
	}

	/**
	 * Updates information about a user
	 * 
	 * @param user
	 *            to be updated
	 */
	@Override
	public void updateUser(User user) {
		session.beginTransaction();
		session.update(user);
		session.getTransaction().commit();
	}

}
